/*
 * Copyright (c) 2007 Aleksey Nikiforov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Aleksey Nikiforov' nor the names of other contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.lex.input.effects.component;


/**
 * This class models spring motion on 1-dimensional axis.
 * Using Runge-Kutta algorithm makes this spring almost framerate-independent.
 * 
 * @author lex
 */
public class HiRezSpringComponent extends SpringComponent {

	public HiRezSpringComponent(SpringConfig config) {
		super(config);
	}

	/**
	 * Computes next values for x and v using Runge-Kutta algorithm.
	 * @param h the time since the last update
	 */
	public void updateSpring(float h) {
		float ax = v;
		float av = vp(x, v);
		
		float bx = v + h/2*av;
		float bv = vp(x + h/2*ax, bx);
		
		float cx = v + h/2*bv;
		float cv = vp(x + h/2*bx, cx);
		
		float dx = v + h*cv;
		float dv = vp(x + h*cx, dx);
		
		newx = x + h/6*(ax + 2*bx + 2*cx + dx);
		newv = v + h/6*(av + 2*bv + 2*cv + dv);
	}

	/**
	 * Read vp as v', this will make it easy to understand the computations
	 * if you are familiar with Runge-Kutta algorithm.
	 * 
	 * @param x
	 * @param v
	 * @return v'
	 */
	private float vp(float x, float v) {
		return - config.k/config.m*x - config.b/config.m*v;
	}

	/*
	 * This set writes out Runge-Kutta calculation explicitly in
	 * functional notation. It may be easier to understand but
	 * less efficient.
	private void computeRungeKutta(float h) {
		float ax = xp(x, v);
		float av = vp(x, v);
		
		float bx = xp(x + h/2*ax, v + h/2*av);
		float bv = vp(x + h/2*ax, v + h/2*av);
		
		float cx = xp(x + h/2*bx, v + h/2*bv);
		float cv = vp(x + h/2*bx, v + h/2*bv);
		
		float dx = xp(x + h*cx, v + h*cv);
		float dv = xp(x + h*cx, v + h*cv);
		
		x = x + h/6*(ax + 2*bx + 2*cx + dx);
		v = v + h/6*(av + 2*bv + 2*cv + dv);
	}

	private float xp(float x, float v) {
		return v;
	}
	
	private float vp(float x, float v) {
		return - k / m * x - b / m * v;
	}
	*/
}
